import axios from "@/utils/http";
import { ElMessage } from "element-plus";

//数据
const state = {
    //获取添加的数据
    tableData:[],
    //搜索的数据getAddData
    search:[],
    //获取所有数据
    allData:[],
    //获取点击行
    focusRow:{},

    //订单编号
    newOrderNumber:0,

  };
  
  const getters = {
    //获取添加的数据
    getAddData(state:any){
        let arr = state.tableData
        console.log(arr);
        return arr
    },

    //获取搜索的数据
    getSearchData(state:any,str:any){
      let arr = state.search
      console.log(arr);
      return arr
    },

     //获取所有的数据
     getAllData(state:any){
      let arr = state.allData
      console.log(arr);
      return arr
    },

    //表格里面的计算
    totalPres(state:any){
      state.allData.forEach((item:any) => {
        item.trades = item.trade*item.drugNumber
        item.pres = item.prescription*item.drugNumber
      });
      return null;
    }
    ,

    /* ————————————————————————— 计算批发总金额函数 ————————————————————————— */
    getSholesale(state:any){
      //循环批发额的计算
      let wholesaleSum = 0 ;
      for (let i = 0; i < state.allData.length; i++) {
        console.log(state.allData[i].trades);
        wholesaleSum += state.allData[i].trades;
      }
      console.log('wholesaleSum',wholesaleSum);
      return wholesaleSum;
    },

    /* ————————————————————————— 计算处方总金额函数 ————————————————————————— */
    getPresption(state:any){
      //循环处方额的计算
      let Prescription = 0 ;
      for (let i = 0; i < state.allData.length; i++) {
        console.log(state.allData[i].pres);
        Prescription += state.allData[i].pres;
      }
      console.log('wholesaleSum',Prescription);
      return Prescription;
    },

   
   
  };
  
  const actions = {};
  
  //获取添加的 id
  const mutations = {
    setFocus(state:any,focusRow:any){
      state.focusRow=focusRow.id
      console.log(state.focusRow);
      
    },
    
    //获取添加输入的数量
    changeNumber(state:any,num:any){
      state.allData.forEach((item:any) => {
        if(item.id==state.focusRow){
          item.drugNumber=num
        }
      });
    },

    //获取输入的处方价
    changePrice1(state:any,num:any){
      state.allData.forEach((item:any) => {
        if(item.id==state.focusRow){
          item.trade=num
        }
      });
    },

    //获取输入的批发价
    changePrice2(state:any,num:any){
      state.allData.forEach((item:any) => {
        if(item.id==state.focusRow){
          item.prescription=num
        }
      });
      
    },
    /* ———————————————————————————— 获取添加的数据 ———————————————————————————— */
    setAddData(state:any){
        //此处写axios请求拿数据覆盖原本死数据
        axios({
            url: "/medicine/message/findDrugList",
            method: "get",
            params: {
              currentPage: 1,
              pageSize: 5,
              status: 1,
            }
          }).then((res) => {
            //获取到的数据
            state.tableData = res.data.data;
            console.log("state.tableData",state.tableData);
          });
    },

    /* ———————————————————————————— 添加里面的搜索 ———————————————————————————— */
    setSearchData(state:any,str:any){
      console.log(str);
      //发请求
        axios({
          url: "/medicine/purchase/findDrugById",
          method: "get",
          params: {
            drug_id: str.str,
          },
        })
          .then((res) => {
            state.tableData = res.data.data;
            console.log("查询信息", res.data.data);
            
          })
          .catch((err) => {
            console.log(err);
          });
    },
  
    /* ———————————————————————————— 获取所有的数据 ———————————————————————————— */
    setAllData(state:any){
      //此处写axios请求拿数据覆盖原本死数据
      axios({
          url: "/medicine/purchase/findOrderByBillId",
          method: "get",
          
        }).then((res) => {
          //获取到的数据
          let arr = JSON.parse(JSON.stringify(res.data.data));
          state.allData.drugNumber = res.data.data.drugNumber;
          // let a = toRefs(state.allData.drugNumber);
          // console.log(a);
          

          // let a = arr[arr.length - 1].billId + 1
          console.log('arr',arr);
          
          // state.newOrderNumber= a;
          // console.log(state.newOrderNumber);
          
          console.log("state.allData",state.allData);
        });
    },

     /* ————————————————————————— 清空数值函数 ————————————————————————— */
    setClear(state:any){
      //清空表格的数据
      state.allData = [];
    },

    //把数据拼接
    saveDrugData(state:any,data:any){
      let arr = <any>[];
      let dataArr = JSON.parse(JSON.stringify(data.value))
      dataArr.forEach((item:any) => {
        arr.push({
          id:item.id,
          drugName:item.drugName,
          drugFactory:item.drugFactory,
          drugUnit:item.drugUnit,
          drugSort:item.drugSort,
          drugSpecification:item.drugSpecification,
          drugNumber:0,
          trade:0,
          prescription:0,
          trades:0,
          pres:0,
        })
      });
      state.allData=arr; 
    },

    /* ————————————————————————— 提交审核 ————————————————————————— */
    setCommit(state:any){
        //拿到该条添加的数据
        let SubmitValue = state.focusRow.value;
        console.log('SubmitValue',SubmitValue);
        //发请添加请求
        axios({
          url:'',
          method:'post',
          params:{
            drugName:'西安',
            drugUnit:'西安',
            drugFactory:'西安',
            drugNumber:20,
            trade:12,
            prescription:12,
            trades:12,
            pres:12,
          }
        }).then( (res)=> {
          console.log(res);
          
        }).catch( (err)=> {
          console.log(err);
        })     
    }
  
   


    
    
  };
  
  export default {
    state,
    mutations,
    actions,
    getters,
  };

function toRefs(drugNumber: any) {
  throw new Error("Function not implemented.");
}
  